<?php
namespace common\services;

use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\ExhibitionBusiness;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ExhibitionBusinessServices extends ExhibitionBusiness
{

    /**
     * 返回对应展会的展商构成
     * @param $exhibition_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getExhibitionBusiness($exhibition_id) {
        $model = ExhibitionBusiness::find(false);
        $model->andFilterWhere(['=', 'status', ExhibitionBusiness::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'exhibition_id', $exhibition_id]);

        $res = $model->asArray()->select("title,num")->orderBy("order desc,id asc")->all();

        return $res;
    }

    /**
     * 保存展商构成数据
     * @param $exhibition_id
     * @param $data
     * @return bool
     */
    public function saveBusinessData($exhibition_id, $data) {
        //将对应展会数据设置为停用
        ExhibitionBusiness::updateAll(['status' => ExhibitionBusiness::STATUS_FLAG_STOP],'exhibition_id=:exhibition_id', [':exhibition_id' => $exhibition_id]);
        if (empty($data)) return false;
        if (!is_array($data)) {
            $data = json_decode($data, true);
        }

        $batchInsert = [];
        foreach ($data as $val) {
            $model = new ExhibitionBusiness();
            $model->exhibition_id = $exhibition_id;
            $model->title = $val['title'];
            $model->num = $val['num'];
            array_push($batchInsert, $model);
        }
        //批量添加数据
        if (!empty($batchInsert) ) {
            BaseActiveRecord::batchInsertSqlArray($batchInsert);
        }
        return true;
    }

}
